<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <!-- logo -->
        <img src="@/assets/heima.png">
        <!-- 顶部标题 -->
        <span>东京电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <el-aside :width="isCollapse?'64px':'200px'">
        <!-- 侧边栏 -->
        <div class="toggle-button" @click="toggleCollapse">
          <i class="el-icon-s-unfold" v-if="isCollapse"></i>
          <i class="el-icon-s-fold" v-else></i>
        </div>
        <!-- collapse  控制折叠效果以及动画 -->
        <!-- 路由属性router 简写默认为true => router     path属性跳转路径 -->
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFF" unique-opened
          :collapse='isCollapse' :collapse-transition='false' :router="true" :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="obj.id+''" v-for="obj in menusList" :key="obj.id">
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[obj.id]"></i>
              <!-- 文本 -->
              <span slot="title">{{obj.authName}}</span>
            </template>
            <!-- 二级子菜单 -->
            <el-menu-item :index="'/'+item.path" v-for="item in obj.children" :key="item.id"
              @click="saveActivePath('/'+item.path)">
              <!-- 二级菜单模板 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span slot="title">{{item.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 路由坑位 -->
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'home',
  data () {
    return {
      isCollapse: false,   // 是否折叠
      menusList: [],    // 左侧数据
      iconsObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      activePath: '',         // 进行路径绑定
    }
  },
  methods: {
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      // 请求左侧数据
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status != 200) return this.$message.error(res.meta.error)
      this.menusList = res.data
      // console.log(res);
    },
    // 点击保存链接的激活状态
    saveActivePath (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () {
    this.getMenuList()
    // 默认点击获取绑定属性高亮状态
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  mounted () { }
}
</script>

<style lang='less' scoped>
.home-container {
  height: 100%;
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    margin-left: 20px;
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
    img {
      filter: invert(100%);
    }
  }
}
.iconfont {
  margin-right: 10px;
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
